package cn.neusoft.controller;

import cn.neusoft.po.CartItem;
import cn.neusoft.service.ICartService;
import cn.neusoft.vo.ResponseObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    ICartService cartService;

    @PostMapping("/{uid}")
    public ResponseObj addCart(@PathVariable("uid") Integer userId, @RequestBody CartItem item){
        boolean b = cartService.addCart(userId, item);
        return b?ResponseObj.SUCCESS("添加购物车成功"):ResponseObj.ERROR(11001,"添加购物车失败");
    }

    @GetMapping("/{uid}")
    public ResponseObj getCart(@PathVariable("uid") Integer userId){
        Collection<CartItem> cart = cartService.getCart(userId);
        return ResponseObj.SUCCESS(cart);
    }

    @PostMapping("/change/{uid}")
    public ResponseObj setCart(@PathVariable("uid") Integer userId,@RequestBody List<CartItem> cartItems){
       boolean b = cartService.setCart(userId,cartItems);
        return b?ResponseObj.SUCCESS("更新购物车成功"):ResponseObj.ERROR(11003,"更新购物车失败");
    }

}
